package com.yzh.service.impl;

import com.yzh.service.*;
import com.yzh.util.Constants;

/**
 * @author 于振晗
 * @title: GuessGame
 * @description: TODO 游戏实现
 * @date 2020年08月29日
 * @path com.yzh.service.impl
 */
public class GuessGame implements GameInterface {
    private Player userPlayer;
    private Player computerPlayer;

    private int count;


    @Override
    public void initGame() {
        userPlayer = new UserPlayer("阿尔法狗");
        computerPlayer = new ComputerPlayer("小三");
        count = 1;
        System.out.println("游戏初始化");
    }

    @Override
    public void startGame() {
        while(true){
            System.out.println("当前游戏第"+count+"局!");
            //获取用户出拳
            int userValue = userPlayer.getInputValue();
            //获取电脑出拳
            int computerValue = computerPlayer.getInputValue();
            //打印出拳结果
            System.out.println(userPlayer.getName()+":"+ Constants.NAME[userValue]+"\t"+computerPlayer.getName()+":"+Constants.NAME[computerValue]);
            //裁判宣布结果 1 用户赢 -1电脑赢
            int result = Judger.compare(computerValue,userValue);
            //根据结果修改用户的胜利场次
            updWinCount(result);
            //输出本局结果
            showResult(result);
            if (userPlayer.getWinCount() == Constants.WIN_COUNT || computerPlayer.getWinCount() == Constants.WIN_COUNT) {
                break;
            }
            System.out.println("\n");
            count++;
        }
    }

    //修改胜利次数
    private void updWinCount(int result){
        if(result == 1){
            userPlayer.setWinCount(userPlayer.getWinCount()+1);
        }else if(result == -1){
            computerPlayer.setWinCount(computerPlayer.getWinCount()+1);
        }
    }

    private void showResult(int result){
        switch (result){
            case 0:
                System.out.println("本局:和局");
                break;
            case 1:
                System.out.println("本局:"+userPlayer.getName()+"赢");
                break;
            case -1:
                System.out.println("本局:"+computerPlayer.getName()+"赢");
                break;
        }
        //打印当前结果战绩
        System.out.println("当前比分为:"+userPlayer.getName()+"["+userPlayer.getWinCount()+"]:["+computerPlayer.getWinCount()+"]"+computerPlayer.getName());

    }

    @Override
    public void endGame() {
        System.out.println("==========================================");
        System.out.println("最终比分为:"+userPlayer.getName()+"["+userPlayer.getWinCount()+"]:["+computerPlayer.getWinCount()+"]"+computerPlayer.getName());
        if(userPlayer.getWinCount() == Constants.WIN_COUNT){
            System.out.println(userPlayer.getName()+"胜利");
        }else{
            System.out.println(computerPlayer.getName()+"胜利");
        }
    }
}
